/**
 * 
 */
package com.risetek.tft.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.risetek.tft.context.Constants;
import com.risetek.tft.entity.TManager;
import com.risetek.tft.entity.TMenu;
import com.risetek.tft.service.MenuService;
import com.zlinepay.framework.bean.TreeNode;
import com.zlinepay.framework.service.impl.EntityServiceImpl;

/**
 * @author  heato
 *
 */
@Service
public class MenuServiceImpl extends EntityServiceImpl<TMenu> implements
		MenuService {
	
	public List<TMenu> listRoot() {
		String hql = "from TMenu a where a.TMenu is null order by a.ordno";
		return find(hql);
	}
	
	public List<TMenu> getAll() {
		String hql = "from TMenu a order by a.ordno";
		return find(hql);
	}
	
	public List<TreeNode> listTree(TManager manager) {
		if(manager.getIssuper()) {
			return createTree(find("from TMenu a where a.TMenu is null order by a.ordno"), null);
		} else {
			return createTree(getChildMenuInPermission(null, manager.getManagerid()), manager.getManagerid());
		}
	}

	private List<TreeNode> createTree(List<TMenu> l, Integer accountid) {
		List<TreeNode> tree = new ArrayList<TreeNode>();
		String hql = "from TMenu a where a.TMenu.id=? and a.state=1 order by a.ordno";
		List<TMenu> list = null;
		for (TMenu a : l) {
			TreeNode t = new TreeNode();
			t.setId(a.getId().toString());
			t.setText(a.getName());
			Map map  = new HashMap();
			map.put("img", a.getImg());
			t.setAttributes(map);
			if(accountid == null) {
				try {
					list = find(hql, a.getId());
				}catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				list = getChildMenuInPermission(a.getId(), accountid);
			}
			if (!list.isEmpty()) {
				t.setChildren(createTree(list, accountid));
			}
			tree.add(t);
		}
		return tree;
	}
	
	public List<Map<String, Object>> getMenuExclude(Integer menuid) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new map(a.id as id,a.TMenu.id as pid,a.name as name,a.nlevel as nlevel) from TMenu a where 1=1");
		if(menuid != null) {
			hql.append(" and a.scort not like ?");
		}
		hql.append(" order by a.scort,a.ordno");
		if(menuid != null) {
			return find(hql.toString(), "%,"+menuid+",%");
		}
		return find(hql.toString());
	}
	
	@SuppressWarnings("unchecked")
	public List<TMenu> getChildMenuInPermission(Integer menuid, Integer accountid) {
		String hql = "select a from TMenu a join a.TOperates b join b.TRoles c join c.TManagers d where d.managerid=? and b.id.op='list' and a.state=1";
		if(menuid == null) {
			hql += " and a.TMenu is null order by a.ordno";
			return find(hql, accountid);
		} else {
			hql += " and a.TMenu.id=? order by a.ordno";
			return find(hql, accountid, menuid);
		}
	}
	
	/*
	public List<FarmMenu> getAllInPermission(Integer menuid, Integer accountid) {
		List<FarmMenu> result = null;
		String hql = "select a.farmMenu from FarmOperate a,FarmRole b,FarmManager c where c.id=? and c.farmRole.id=b.id and exists elements (b.farmMenus) and a = some elements(b.farmOperates) and b = some elements (c.farmRoles) and a.state=1";
		if(menuid == null) {
			hql += " and a.farmMenu.farmMenu is null order by a.ordno";
			result = find(hql, accountid);
		} else {
			hql += " and a.farmMenu.farmMenu.id=? order by a.ordno";
			result = find(hql, accountid, menuid);
		}
		for(FarmMenu menu : result) {
			
		}
	}
	*/
	
	public void setOrder(Integer id, Integer order) {
		String hql = "UPDATE TMenu t set t.ordno=? where t.id=?";
		update(hql, order, id);
	}
	
	public void resetOrder() {
		String hql = "UPDATE TMenu t set t.ordno=?";
		update(hql, Constants.ORDERNO);
	}
}
